home *** CD-ROM | disk | FTP | other *** search
/ CICA 1993 April / CICA MS Windows - April 1993.iso / bbs / wildcat / install.bak < prev    next >
Text File  |  1993-04-19  |  8KB  |  386 lines

  1. /*
  2.  * install.c
  3.  * -force option
  4.  *
  5.  * cica version
  6.  */
  7.  
  8. #include <ctype.h>
  9. #include <conio.h>
  10. #include <io.h>
  11. #include <fcntl.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15.  
  16. #if 0
  17.   FileAreaRecType = record
  18.                       AreaName       : Str30;
  19.                       AreaPath       : array[1..4] of DirStr;
  20.                     end;
  21.  
  22. #endif
  23.  
  24. // don't pack structures
  25. #pragma -a-
  26. struct file_area_rec_type {
  27.     unsigned char length;
  28.     char area_name[30];
  29.     struct {
  30.         unsigned char length;
  31.         char path[67];
  32.     } area_path[4];
  33. };
  34.  
  35. struct make_wild_record {
  36.     char dummy1[0x321];
  37.     int max_file_areas;
  38. };
  39.  
  40. char letter;
  41. int start_area;        /* xxx */
  42.  
  43. int
  44. highest_area(void) {
  45.     char buf[500];
  46.     int fd;
  47.     int i;
  48.     struct file_area_rec_type area;
  49.     int count = 0;
  50.  
  51.     if (-1 == (fd = open("filearea.dat", O_RDWR | O_BINARY))) {
  52.         fprintf(stderr, "unable to open 'filearea.dat'\n");
  53.         exit(1);
  54.     }
  55.     
  56. //    printf("%d\n", sizeof(struct file_area_rec_type));
  57.  
  58.     for (;;) {
  59.         i = read(fd, &area, sizeof(struct file_area_rec_type));
  60.         if (i == 0)
  61.             break;
  62.  
  63.         if (i != sizeof(struct file_area_rec_type)) {
  64.             fprintf(stderr, "error reading file: (%d) %s\n",
  65.                 i, sys_errlist[errno]);
  66.             exit(1);
  67.         }
  68.         ++count;
  69.         strncpy(buf, area.area_name, area.length);
  70.         if (area.length == 0)
  71.             continue;
  72.         buf[area.length] = 0;
  73. //        printf("area: `%s'\n", buf);
  74.         for (i = 0; i < 4; i++) {
  75.             if (area.area_path[i].length == 0)
  76.                 continue;
  77.             strncpy(buf, area.area_path[i].path,
  78.                     area.area_path[i].length);
  79.             buf[area.area_path[i].length] = 0;
  80. //            printf("path %d: `%s'\n", i, buf);
  81.         }
  82.     }
  83.     
  84.     close(fd);
  85.     
  86.     return(count);
  87. }
  88.  
  89. void
  90. install_areas(void) {
  91.     char vbuf[3000];
  92.     char buf[500];
  93.     int fd;
  94.     int i;
  95.     struct file_area_rec_type area;
  96.     FILE *fh;
  97.     char line[200];
  98.     char description[100];
  99.     char path[100];
  100.     int current_areas;
  101.     char *p;        
  102.  
  103.     if (-1 == (fd = open("filearea.dat", O_RDWR | O_BINARY))) {
  104.         fprintf(stderr, "unable to open 'filearea.dat'\n");
  105.         exit(1);
  106.     }
  107.     
  108.     sprintf(buf, "%c:\\dirs.txt", letter);
  109.     if (NULL == (fh = fopen(buf, "rt"))) {
  110.         fprintf(stderr, "error opening cdrom directory list ``%s'': %s\n",
  111.             buf, sys_errlist[errno]);
  112.         exit(1);
  113.     }
  114.     
  115.     setvbuf(fh, vbuf, _IOFBF, 2998);
  116.  
  117.     current_areas = 0;
  118.     while (1) {
  119.         i = read(fd, &area, sizeof(struct file_area_rec_type));
  120.         if (i == 0)
  121.             break;
  122.  
  123.         if (i != sizeof(struct file_area_rec_type)) {
  124.             fprintf(stderr, "error reading file: (%d) %s\n",
  125.                 i, sys_errlist[errno]);
  126.             exit(1);
  127.         }
  128.         ++current_areas;
  129.     }
  130.  
  131.     memset(&area, 0, sizeof(struct file_area_rec_type));
  132.     /* write null areas into any gap */
  133.     while (current_areas < start_area - 1) {
  134.         i = write(fd, &area, sizeof(struct file_area_rec_type));
  135.  
  136.         if (i != sizeof(struct file_area_rec_type)) {
  137.             fprintf(stderr, "error writing '%s' file: (%d) %s\n",
  138.                 "filearea.dat", i, sys_errlist[errno]);
  139.             fclose(fh);
  140.             exit(1);
  141.         }
  142.         ++current_areas;
  143.     }
  144.     
  145.     while (NULL != fgets(line, 199, fh)) {
  146.         memset(&area, 0, sizeof(struct file_area_rec_type));
  147.         
  148. //        ++current_areas;
  149.         
  150.         p = strtok(line, "\n\r\t ");
  151.         if (!p) {
  152.             fprintf(stderr, "line has no path:\n``%s''\n",
  153.                 line);
  154.             exit(1);
  155.         }
  156.         
  157.         strcpy(path, p);
  158.         if (line[strlen(path) - 1] == '\\')
  159.             line[strlen(path) - 1] = 0;
  160.  
  161.         p = line + strlen(path) + 1;
  162.         while (isspace(*p))
  163.             ++p;
  164.         if (!p) {
  165.             fprintf(stderr, "line has no description:\n``%s''\n",
  166.                 line);
  167.             exit(1);
  168.         }
  169.         strcpy(description, p);
  170.         *(description + strlen(description) - 1) = 0;
  171.         
  172.         sprintf(buf, "%c:%s\\", letter, line);
  173.         strcpy(area.area_path[0].path, buf);
  174.         printf("path[1] = >%s<\n", area.area_path[1].path);
  175.         area.area_path[0].length = (unsigned char)strlen(buf);
  176.  
  177.         i = strlen(description);
  178.         if (i > 30)
  179.             i = 30;
  180.         fprintf(stderr, "installing: %s\n", description);
  181.         strncpy(area.area_name, description, i);
  182.         area.length = (unsigned char)i;
  183.         
  184.         i = write(fd, &area, sizeof(struct file_area_rec_type));
  185.  
  186.         if (i != sizeof(struct file_area_rec_type)) {
  187.             fprintf(stderr, "error writing '%s' file: (%d) %s\n",
  188.                 "filearea.dat", i, sys_errlist[errno]);
  189.             fclose(fh);
  190.             exit(1);
  191.         }
  192.     }
  193.     
  194.     fclose(fh);
  195.     close(fd);
  196. }
  197.  
  198. int
  199. count_areas(void) {
  200.     char vbuf[8000];
  201.     char buf[500];
  202.     char line[200];
  203.     FILE *fh;
  204.     int areas;
  205.  
  206.     sprintf(buf, "%c:\\dirs.txt", letter);
  207.     if (NULL == (fh = fopen(buf, "rt"))) {
  208.         fprintf(stderr, "error opening cdrom directory list ``%s'': %s\n",
  209.             buf, sys_errlist[errno]);
  210.         exit(1);
  211.     }
  212.     
  213.     setvbuf(fh, vbuf, _IOFBF, 7998);
  214.  
  215.     areas = 0;
  216.     while (NULL != fgets(line, 199, fh)) {
  217.         ++areas;
  218.     }
  219.     
  220.     fclose(fh);
  221.     
  222.     return(areas);
  223. }
  224.  
  225. void
  226. build_batch(void) {
  227.     char vbuf[3000];
  228.     char vbuf2[3000];
  229.     char buf[500];
  230.     FILE *fh;
  231.     FILE *output;
  232.     char line[200];
  233.     char path[100];
  234.     int a;
  235.     char *p;        
  236.     
  237.     fprintf(stderr, "Building batch file 'install2.bat'... ");
  238.  
  239.     sprintf(buf, "%c:\\dirs.txt", letter);
  240.     if (NULL == (fh = fopen(buf, "rt"))) {
  241.         fprintf(stderr, "error opening cdrom directory list ``%s'': %s\n",
  242.             buf, sys_errlist[errno]);
  243.         exit(1);
  244.     }
  245.     setvbuf(fh, vbuf, _IOFBF, 2998);
  246.     
  247.     if (NULL == (output = fopen("install2.bat", "wt"))) {
  248.         fprintf(stderr, "error opening \"install2.bat\": %s\n",
  249.             sys_errlist[errno]);
  250.         exit(1);
  251.     }
  252.     setvbuf(output, vbuf2, _IOFBF, 2998);
  253.  
  254.     a = start_area;
  255.     while (NULL != fgets(line, 199, fh)) {
  256.         p = strtok(line, "\n\r\t ");
  257.         if (!p) {
  258.             fprintf(stderr, "line has no path:\n``%s''\n",
  259.                 line);
  260.             exit(1);
  261.         }
  262.         
  263.         strcpy(path, p);
  264.         if (line[strlen(path) - 1] == '\\')
  265.             line[strlen(path) - 1] = 0;
  266.  
  267.         fprintf(output,
  268.             "wcfile /A:%d /S:%c:%s /D:%c:%s\\00_index.txt /U:\"CICA CDROM\" /P:1,12,25,80 /R\n",
  269.         a, letter, path, letter, path);
  270.  
  271.         ++a;
  272.     }
  273.     
  274.     fclose(fh);
  275.     fclose(output);
  276.     fprintf(stderr, "done.");
  277. }
  278.  
  279.  
  280. void
  281. main(char *argv[]) {
  282.     char buf[2000];
  283.     char *p;
  284.     int i;
  285.     int fd;
  286.     int highest_filearea;
  287.     struct make_wild_record rec;
  288.     int force = 0;
  289.     int areas;
  290.     
  291.     if (0 == stricmp(argv[1], "-f"))
  292.         ++force;
  293.  
  294.     highest_filearea = highest_area();
  295.     
  296.     if (-1 == (fd = open("makewild.dat", O_RDWR | O_BINARY))) {
  297.         fprintf(stderr, "unable to open 'makewild.dat'\n");
  298.         exit(1);
  299.     }
  300.     
  301.     i = read(fd, &rec, sizeof(struct make_wild_record));
  302.  
  303.     if (i != sizeof(struct make_wild_record)) {
  304.         fprintf(stderr, "error reading '%s' file: (%d) %s\n",
  305.             "makewild.dat", i, sys_errlist[errno]);
  306.         exit(1);
  307.     }
  308.     
  309.     lseek(fd, 0L, SEEK_SET);
  310.     
  311.     if (rec.max_file_areas != highest_filearea) {
  312.         fprintf(stderr,
  313. "Size of filearea.dat file area database (%d areas) and # areas\n",
  314.             highest_filearea);
  315.         fprintf(stderr, "configured (%d areas) don't match.\n",
  316.             rec.max_file_areas);
  317.         fprintf(stderr, "Set # of new areas with makewild.\n");
  318.         exit(1);
  319.     }
  320.  
  321. again:;
  322.     cprintf("What DOS drive letter is your CDROM drive?\n\r");
  323.     gets(buf);
  324.     p = buf;
  325.     while (isspace(*p))
  326.         ++p;
  327.     letter = *p;
  328.  
  329.     areas = count_areas();
  330.     while (1) {
  331.         cprintf("What area should this CDROM's areas start at?\n\r");
  332.         cprintf("Your highest area # is %d.  This CDROM disc\n\r", 
  333.             highest_filearea);
  334.         cprintf("has %d areas.  A good starting area is %d.\n\r", 
  335.             areas, highest_filearea + 1);
  336.         
  337.         gets(buf);
  338.         p = buf;
  339.         while (isspace(*p))
  340.             ++p;
  341.         
  342.         start_area = atoi(p);
  343.         if (! force && start_area <= highest_filearea) {
  344.           cprintf("Sorry, area %d is already used.  Free areas start\n\r",
  345.               start_area);
  346.             cprintf("with area %d.\n\r", highest_filearea + 1);
  347.             continue;
  348.         }
  349.         
  350.         break;
  351.     }
  352.     
  353.     cprintf("The CDROM drive is DOS drive `%c'.  The new areas will run from\n\r", letter);
  354.     cprintf("area %d to area %d with your last area area %d.\n\r", 
  355.         start_area, start_area + areas, highest_filearea);
  356.     cprintf("If this is correct type 'y'.\n\r");
  357.     
  358.     gets(buf);
  359.     p = buf;
  360.     while (isspace(*p))
  361.         ++p;
  362.     
  363.     if (toupper(*p) != 'Y')
  364.         goto again;
  365.  
  366.     /* write new number of files into global files area */
  367.     rec.max_file_areas = start_area + areas;
  368.     
  369.     i = write(fd, &rec, sizeof(struct make_wild_record));
  370.  
  371.     if (i != sizeof(struct make_wild_record)) {
  372.         fprintf(stderr, "error writing '%s' file: (%d) %s\n",
  373.             "makewild.dat", i, sys_errlist[errno]);
  374.         exit(1);
  375.     }
  376.     
  377.     /* write file descriptions into filearea.dat */
  378.     install_areas();
  379.     
  380.     /* write batch file to add the files */
  381.     
  382.     build_batch();
  383.     
  384.     close(fd);
  385. }
  386.